package sync1;


import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class sync1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SyncThread1 syncThread1 = new SyncThread1();
        SyncThread1 syncThread2 = new SyncThread1();
        Thread thread1 = new Thread(syncThread1, "thread1");
        Thread thread2 = new Thread(syncThread2, "thread2");
        thread1.start();
        thread2.start();

        FutureTask<String> stringFutureTask = new FutureTask<>(() -> {
            return "hello";
        });

        Thread thread = new Thread(stringFutureTask);
        thread.start();
        thread.join();
        System.out.println(stringFutureTask.isDone());;
        System.out.println(stringFutureTask.get());;
    }
}

class SyncThread1 implements Runnable{
    private static int count = 0;

    public SyncThread1() {
    }

    @Override
    public void run() {
        synchronized (this){
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName()+":"+count++);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
